﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;
using Metronome;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        #region
        Int32 bpm = 52;
        public Int32 bpmMin = 30;
        public Int32 bpmMax = 300;
        Int32 millis = baseMil / 52;
        const Int32 baseMil  = 60000;
        Int16 beatMax = 4;
        Int16 beatCnt;
        //declare metronome click .wav's
        SoundPlayer playerClick = new SoundPlayer();
        SoundPlayer playerKlack = new SoundPlayer();
        String clickStr = "click.wav";
        String klackStr = "klack.wav";


        #endregion
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
            //load click sounds to players
            playerClick.SoundLocation = clickStr;
            playerClick.Load();

            playerKlack.SoundLocation = klackStr;
            playerKlack.Load();
                       
            this.lblBeat.Text = Convert.ToString(bpm);
            this.timer1.Enabled = true;
           

            //initialize first bpm and other form stuff
            updateForm();
            //stops first timer instance on first load
            //comment out to automatically play on load
            this.timer1.Stop();

        }

        private void updateForm()
        {
            this.lblBeat.Text = Convert.ToString(bpm); //set new bpm text
            millis = baseMil / bpm;                    //set number of milliseconds per click
            if (this.timer1.Enabled == true)           //if not enabled, then enable and start
            {                                          //and start timer
                this.timer1.Stop();
                this.timer1.Interval = millis;
                this.timer1.Start();
            }
            else 
            {
                this.timer1.Enabled = true;
                updateForm();
            }

        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            if (bpm > bpmMin && bpm < bpmMax)
            {
                bpm -= 1;
                this.updateForm();
            }
            else { }
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            if (bpm >= bpmMin && bpm <= bpmMax)
            {
                bpm += 1;
                

                this.updateForm();
            }
            else { }

            

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (beatCnt == 1)
            {
                playerClick.Play();
                
                label2.Text = Convert.ToString(beatCnt) +
                              "/" + Convert.ToString(beatMax);
                beatCnt++;


            }
            else
            {
                playerKlack.Play();
                if (beatCnt < beatMax)
                {
                    
                    label2.Text = Convert.ToString(beatCnt) +
                              "/" + Convert.ToString(beatMax);
                    beatCnt++;
                }
                else
                {
                   
                    label2.Text = Convert.ToString(beatCnt) +
                              "/" + Convert.ToString(beatMax);
                    beatCnt = 1;
                }

            }
                

            

        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            beatCnt = 1;
            
            this.timer1.Start();


        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            this.timer1.Stop();

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void abtBtn_Click(object sender, EventArgs e)
        {
          AboutMetronome a =  new AboutMetronome();
          a.PerformLayout();
          a.Show();

        }
    }
}
